package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.date.DateUtils;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    @Override
    public void addNewMember(String telephone) {
        // 此方法首先判断数据库是否有此用户,没有则保存此手机用户
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PHONENUMBER",telephone);
        Member member = baseMapper.selectOne(queryWrapper);
        if (member==null){
            Member newMember = new Member();
            newMember.setPhoneNumber(telephone);
            // 设置注册时间 注意:可以在数据库和实体类对应的字段设置属性为Timestamp,(数据库也有此数据类型,插入时如同主键,输入null,自动给值,精确到秒)
            newMember.setRegTime(new Date());
            save(newMember);
        }
    }

    @Override
    public Map getMemberCount(String[] dateScope) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        ArrayList<String> yearAndMonth = new ArrayList<>();
        ArrayList<Integer> memberCounts = new ArrayList<>();
        HashMap<String, List> map = new HashMap<>();
            if (dateScope.length==1){
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.MONTH,-12);
                for (int i = 0; i < 12; i++) {
                    Date date = calendar.getTime();
                    String format = simpleDateFormat.format(date);
                    yearAndMonth.add(format);
                    String begin = format + "-01";
                    String end = format + "-31";
                    Integer memberCount = baseMapper.findMemberCounts(begin,end);
                    memberCounts.add(memberCount);
                    calendar.add(Calendar.MONTH,1);
                }
                map.put("yearAndMonth",yearAndMonth);
                map.put("memberCounts",memberCounts);
                return map;
            }else {
                Date dateStart = null;
                try {
                    dateStart = simpleDateFormat.parse(dateScope[0]);
                } catch (ParseException e) {
                    e.printStackTrace();
                    throw new RuntimeException();
                }
                String dateEnd = dateScope[1];
                Calendar calendarStart = Calendar.getInstance();
                calendarStart.setTime(dateStart);
                calendarStart.add(Calendar.MONTH,-1);
                do {
                    calendarStart.add(Calendar.MONTH,1);
                    Date date = calendarStart.getTime();
                    String format = simpleDateFormat.format(date);
                    yearAndMonth.add(format);
                    String begin = format + "-01";
                    String end = format + "-31";
                    Integer memberCount = baseMapper.findMemberCounts(begin,end);
                    memberCounts.add(memberCount);
                }while (dateEnd.equals(simpleDateFormat.format(calendarStart.getTime()))==false);
                map.put("yearAndMonth",yearAndMonth);
                map.put("memberCounts",memberCounts);
                return map;
            }
    }

    @Override
    public List<Map> getMemberCountWithSex(String[] dateScope) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String begin = null;
        String end = null;
        if (dateScope.length==1){
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH,-1);
            Date endDate = calendar.getTime();
            calendar.add(Calendar.MONTH,-11);
            Date beginDate = calendar.getTime();
            String beginFormat = simpleDateFormat.format(beginDate);
            String endFormat = simpleDateFormat.format(endDate);
            begin = beginFormat + "-01";
            end = endFormat + "-31";
        }else {
            begin = dateScope[0] + "-01";
            end = dateScope[1] + "-31";
        }
        List<Map> mapList = baseMapper.getMemberCountWithSex(begin,end);
        for (Map map : mapList) {
            if ("1".equals((String)map.get("name"))){
                map.put("name","男");
            }else map.put("name","女");
        }
        return mapList;
    }

    @Override
    public List<Map> getMemberCountWithAge(String[] dateScope) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String begin = null;
        String end = null;
        if (dateScope.length==1){
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH,-1);
            Date endDate = calendar.getTime();
            calendar.add(Calendar.MONTH,-11);
            Date beginDate = calendar.getTime();
            String beginFormat = simpleDateFormat.format(beginDate);
            String endFormat = simpleDateFormat.format(endDate);
            begin = beginFormat + "-01";
            end = endFormat + "-31";
        }else {
            begin = dateScope[0] + "-01";
            end = dateScope[1] + "-31";
        }
        List<String> idcardList = baseMapper.getMemberIdcard(begin,end);
        System.out.println(idcardList.toString());;
        List<Map> mapList = new ArrayList<>();
        Map map01 = new HashMap<>();
        Map map02 = new HashMap<>();
        Map map03 = new HashMap<>();
        Map map04 = new HashMap<>();
        map01.put("name","0-18");
        map02.put("name","18-30");
        map03.put("name","30-45");
        map04.put("name","45以上");
        Integer count01 = 0;
        Integer count02 = 0;
        Integer count03 = 0;
        Integer count04 = 0;
        for (String idcard : idcardList) {
            String birth = idcard.substring(6, 10);
            Integer age = Calendar.getInstance().get(Calendar.YEAR)-Integer.parseInt(birth);
            if (age>0&&age<=18){
                count01++;
            }else if (age>18&&age<=30){
                count02++;
            }else if (age>30&&age<=45){
                count03++;
            }else {
                count04++;
            }
        }
        map01.put("value",count01);
        map02.put("value",count02);
        map03.put("value",count03);
        map04.put("value",count04);
        Collections.addAll(mapList,map01,map02,map03,map04);
        return mapList;
    }
}
